#!/bin/sh
# 设定为备用数据库
DataDir=/data/mysql
ScriptDir=$(dirname $0)
LogDir=/App/logs
LogFile=$LogDir/drbd.log

test -d $LogDir || mkdir -p $LogDir

if [ -d "$DataDir" ]; then
    date +'%Y-%m-%d %H:%M:%S' >> $LogFile
    $ScriptDir/mysql stop
    echo "停止数据库($?)" >> $LogFile
    sleep 1
    fuser -ck $(dirname $DataDir)
    umount -l $(dirname $DataDir)
    echo "卸载DRBD($?)" >> $LogFile
    $ScriptDir/drbd slave
    echo "切换为次DRBD($?)" >> $LogFile
else
    drbd_status=$(service drbd status | grep "StandAlone" | awk '{print $2}')
    if [ -n "$drbd_status" ]; then
        date +'%Y-%m-%d %H:%M:%S' >> $LogFile
        drbdadm secondary all
        echo "切换为次DRBD($?)" >> $LogFile
    fi
fi
